package com.jc.repository.configure;

import com.taobao.tddl.matrix.jdbc.TDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;

@Configuration
@ConditionalOnClass({DataSource.class })
@ConditionalOnMissingBean(DataSource.class)
@EnableConfigurationProperties(DataSourceProperties.class)
public class DataSourceAutoConfiguration {
	
	private static Log log = LogFactory.getLog(DataSourceAutoConfiguration.class);
	
	@Autowired
	private DataSourceProperties properties;
	
//	@Bean
//	public DataSource DataSource(){
//		ComboPooledDataSource dataSource = new ComboPooledDataSource();
//		try {
//			dataSource.setDriverClass(properties.getDriverClass());
//			dataSource.setJdbcUrl(properties.getUrl());
//			dataSource.setUser(properties.getUsername());
//			dataSource.setPassword(properties.getPassword());
//			dataSource.setInitialPoolSize(properties.getInitialPoolSize());
//			dataSource.setMinPoolSize(properties.getMinPoolSize());
//			dataSource.setMaxPoolSize(properties.getMaxPoolSize());
//			dataSource.setMaxIdleTime(properties.getMaxIdleTime());
//			dataSource.setIdleConnectionTestPeriod(properties.getIdleConnectionTestPeriod());
//			dataSource.setPreferredTestQuery(properties.getPreferredTestQuery());
//			dataSource.setAcquireIncrement(properties.getAcquireIncrement());
//			dataSource.setAcquireRetryAttempts(properties.getAcquireRetryAttempts());
//		} catch (Exception e) {
//			log.error("datasource init error", e);
//		}
//		return dataSource;
//	}


	@Bean(initMethod = "doInit",destroyMethod = "doDestroy")
	public DataSource DataSource(){
		TDataSource tDataSource = new TDataSource();
		tDataSource.setAppName("jc_core");
		tDataSource.setTopologyFile("matrix/server_topology.xml");
		tDataSource.setRuleFile("V0#classpath:matrix/mysql_rule.xml");
		tDataSource.setSchemaFile("matrix/mysql_schema.xml");
		tDataSource.setSequenceFile("matrix/mysql_sequence.xml");
		return tDataSource;
	}
}
